<?php
use PFT\Db;
if ($_SESSION['sid']!=1) {
    exit('{"status":"fail","msg":"权限验证失败"}');
}
$conf = include '../module/common/db.conf.php';
include '../module/common/Db.class.php';
include '../common/func.inc.php';

$action = safetxt($_REQUEST['action']);
//获取服务器数据
Db::Conf($conf['localhost']);
$con=Db::Connect();
//保存表单
if($action=='save') {
    $fid               = intval($_POST['accountid']);
    $name              = safetxt($_POST['accountname']);
    $adstatus          = safetxt($_POST['adstatus']);
    $forbiden          = safetxt($_POST['forbiden']);
    $max_debt          = safetxt($_POST['max_debt']);
    $delay_days        = safetxt($_POST['delay_days']);
    $proof_price       = safetxt($_POST['proof_price']);
    $pricing_package   = safetxt($_POST['pricing_package']);
    $package_order_num = safetxt($_POST['package_order_num']);
    $package_startdate = safetxt($_POST['package_startdate']);
    $package_enddate   = safetxt($_POST['package_enddate']);
    //判断记录是否已存在
    $sql="select id from pft_account_set where fid=? LIMIT 1";
    $exist_id = Db::fetch($sql,array($fid));

    $param = array(
        $name,
        $adstatus,
        $forbiden,
        $max_debt,
        $delay_days,
        $proof_price,
        $pricing_package,
        $package_order_num,
        $package_startdate,
        $package_enddate,
    );
    if($exist_id) {
        $sql= <<<SQL
UPDATE pft_account_set SET name=?,adstatus=?,forbiden_reason=?,
max_debt=?,arrears_days=?,proof_price=?,package_order_num=?,package_price=?,
package_startdate=?,package_enddate=? where id=$exist_id LIMIT 1
SQL;
    }
    else {
        array_unshift($param, $fid);
        //数据表中无此id时，直接插入
        $sql=<<<SQL
INSERT INTO pft_account_set(fid,name,adstatus,forbiden_reason,max_debt,
arrears_days,proof_price,package_order_num,package_price,package_startdate,
package_enddate)values(?,?,?,?,?,?,?,?,?,?,?)
SQL;
    }
    $ret = Db::execute($sql, $param);
    if($ret===true) {
        echo '{"outcome":1,"msg":"保存成功！"}';
    } else {
        echo '{"outcome":-1,"msg":"保存失败,错误信息："}';
    }
}
elseif ($action=='get_info') {
    $sql = "SELECT * FROM pft_account_set WHERE fid=? LIMIT 1";
    $row = Db::fetch($sql, array(intval($_GET['fid'])));
    if ($row) {
        echo json_encode($row);
    } else {
        echo '[]';
    }
}
?>